/* 
 * 
 * Created on 14 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package gui.filetree;

import gui.GUIUtils;

import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import ssh.SSHUtils;


/**
 *
 * @author simonk
 */
public class FileTreeExpansionListener implements TreeExpansionListener {
	public void treeExpanded(TreeExpansionEvent evt) {
		final JTree tree = (JTree) evt.getSource();
		final TreePath path = evt.getPath();
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				System.out.println("treeExpanded");
				final TreeNode node = (TreeNode) path.getLastPathComponent();
				if (node.getChildCount() == 0) {

					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							String dir = "~";
							for (int i = 1; i < path.getPath().length; i++) {
								dir += "/" + path.getPath()[i].toString();
								System.out.println("dir " + dir);
							}
							GUIUtils.MainPanel.setMessageLbl("Retrieving files from file server");
							SSHUtils.buildFileSystem(dir, node);
							/*DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
							int dirIndex = 0;
							for(int i = 0; i < node.getChildCount(); i++) {
								TreeNode childNode = (TreeNode) node.getChildAt(i);
								model.insertNodeInto(childNode, node, childNode.isDirectory() ? dirIndex++ : i);
							}*/
						}
					});
				}
			}
		});
	}

	public void treeCollapsed(TreeExpansionEvent evt) {
		// JTree tree = (JTree) evt.getSource();
		// TreePath path = evt.getPath();
		// System.out.println("treeCollapsed");
	}
}
